package weibo.service;

import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.CORBA.portable.InputStream;

/**
 * Servlet implementation class image
 */
@WebServlet("/image")
public class image extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public image() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * 查看图片
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		//通过指定URL访问图片
				response.setContentType("image/png");
				if (request.getParameter("src")!=null){
					try{
						String Path=System.getProperty("user.dir")+"/img/"+request.getParameter("src");
						FileInputStream in=new FileInputStream(Path);
						byte[] data = new byte[in.available()];
						in.read(data);
						in.close();
						OutputStream out=response.getOutputStream();
						out.write(data);
						out.flush();
						out.close();
					}catch(Exception e){
						e.printStackTrace();
						System.out.println(request.getParameter("src")+" 访问了无效的图片");
						response.sendRedirect("./img/defaulthead.png");
					}
					
				}else{
					response.sendRedirect("./img/defaulthead.png");
				}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
